﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 对象池
{
    class Program
    {
        static void Main(string[] args)
        {
           Queue<MyConnection> Queue_m=new Queue<MyConnection>();


        
               for (int i = 2 - 1; i >= 0; i--)
               {
                   Thread createThread = new Thread(() =>
                   {
                       while (true)
                       {
                           lock ("bz")
                           {

                               MyConnection t = new MyConnection();
                               Queue_m.Enqueue(t);
                               Console.WriteLine("生产" + t.AsGuid);

                           }
                           Thread.Sleep(200);
                       }
                      
                           
                   });
                   createThread.IsBackground = true;
                   createThread.Start();

               }
               for (int i = 3 - 1; i >= 0; i--)
               {
                   Thread sellThread = new Thread(() =>
                   {

                       while (true)
                       {
                           lock ("bz")
                           {
                               if (Queue_m.Count > 0)
                               {
                                   MyConnection t = Queue_m.Dequeue();
                                   Console.WriteLine("----消费了" + t.AsGuid);

                               }
                           }
                           Thread.Sleep(500);  
                       }
                           
                      

                   });
                   sellThread.IsBackground = true;
                   sellThread.Start();
               }

                    Console.ReadKey();






        }
        public class MyConnection
        {
            private Guid asGuid = Guid.NewGuid();

            public Guid AsGuid
            {
                get { return asGuid; }
                set { asGuid = value; }
            }
        }
    }
}
